/**
 * @copyright (c) 2024, MacRsh
 *
 * @license SPDX-License-Identifier: Apache-2.0
 *
 * @date 2024-08-03    MacRsh       First version
 */

#ifndef __HASHMAP_H__
#define __HASHMAP_H__

#include <errno.h>
#include <malloc.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @addtogroup Hashmap
 * @{
 */

/**
 * @struct HashNode
 * @brief Represents a node in the hash map.
 */
typedef struct HashNode {
    uint32_t key;
    void *value;
    struct HashNode *next;
} HashNode_t;

/**
 * @struct HashMap
 * @brief Represents a hashmap.
 */
typedef struct HashMap {
    HashNode_t **buckets;
    size_t capacity;
    size_t count;
    float load_factor;
    float shrink_factor;
} HashMap_t;

int hashmap_init(HashMap_t *map, size_t capacity, float load_factor);
void hashmap_deinit(HashMap_t *map);
HashMap_t *hashmap_new(size_t capacity, float load_factor);
void hashmap_del(HashMap_t *map);
int hashmap_insert(HashMap_t *map, uint32_t key, void *value);
void *hashmap_remove(HashMap_t *map, uint32_t key);
void hashmap_clear(HashMap_t *map);
void *hashmap_get(HashMap_t *map, uint32_t key);
size_t hashmap_len(HashMap_t *map);
bool hashmap_is_empty(HashMap_t *map);
size_t hashmap_capacity(HashMap_t *map);
int hashmap_reserve(HashMap_t *map, size_t reserve_size);
int hashmap_shrink_to(HashMap_t *map, size_t capacity);
int hashmap_shrink_to_fit(HashMap_t *map);
bool hashmap_contains_key(HashMap_t *map, uint32_t key);
uint32_t hash_string(const char *str);

/** @} */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __HASHMAP_H__ */
